package com.transfer.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.staff.entity.Staff;
import com.staff.service.StaffService;
import com.transfer.dao.PersonTransferRepository;
import com.transfer.entity.PersonTransfer;
import com.transfer.entity.dto.PersonTransferQueryDTO;

@Service
@Transactional
public class PersonTransferService implements IPersonTransferService {
	@Autowired
	private PersonTransferRepository personTransferRepository;
	@Autowired
	private StaffService staffService;
	@Autowired
	private IPersonTransferService personTransferService;
	

	public void saveOrUpdate(PersonTransfer personTransfer) {
		personTransferRepository.save(personTransfer);
	}

	public void saveOrUpdate(PersonTransferQueryDTO personTransferQueryDTO) {
		PersonTransfer sr = new PersonTransfer();
		if(personTransferQueryDTO.getStaffNum() != null) {
			Staff staff = staffService.findOne(personTransferQueryDTO.getStaffNum());
			if(personTransferQueryDTO.getStaffId() != null) {
				sr = personTransferService.findOne(personTransferQueryDTO.getId());
			}else {
				//
			}
			sr.setStaffNum(personTransferQueryDTO.getStaffNum());
			sr.setStaffName(personTransferQueryDTO.getStaffName());
			sr.setOriginalDepa(personTransferQueryDTO.getOriginalDepa());
			sr.setOriginalPosi(personTransferQueryDTO.getOriginalPosi());
			sr.setNewDepa(personTransferQueryDTO.getNewDepa());
			sr.setNewPosi(personTransferQueryDTO.getNewPosi());
			sr.setDeployDate(personTransferQueryDTO.getDeployDate());
			sr.setStaff(staff);
			
			personTransferService.saveOrUpdate(sr);
		}else {
			//
		}
		
	}

	public void delete(PersonTransfer personTransfer) {
		personTransfer=personTransferRepository.findOne(personTransfer.getId());
		if(personTransfer!=null) {
			personTransfer.setStaff(null);
			personTransferRepository.save(personTransfer);
		}
		personTransferRepository.delete(personTransfer);
	}


	public PersonTransfer findOne(Long id) {
		return personTransferRepository.findOne(id);
	}


	public List<PersonTransfer> findAll() {
		return (List<PersonTransfer>) personTransferRepository.findAll();
	}


	public void delete(List<Long> ids) {
		List<PersonTransfer> personTransferList = (List<PersonTransfer>)personTransferRepository.findAll(ids);
		for(PersonTransfer personTransfer : personTransferList) {
			personTransfer.setStaff(null);
			personTransferRepository.save(personTransfer);
		}
		personTransferRepository.delete(personTransferList);
		
	}


	public Page<PersonTransferQueryDTO> findAll(Specification<PersonTransfer> spec, Pageable pageable) {
		//将人事调动page转化为人事调动DTOpage
		List<PersonTransferQueryDTO> results = new ArrayList<PersonTransferQueryDTO>();
		Page<PersonTransfer> page = personTransferRepository.findAll(spec, pageable);
		for(PersonTransfer sc:page.getContent()) {
			PersonTransferQueryDTO scDTO = new PersonTransferQueryDTO(sc, sc.getStaff());
        	results.add(scDTO);
		}
		return new PageImpl<PersonTransferQueryDTO>(results, pageable, results.size());
	}



}
